﻿using MyBatis.DataMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyBatis.DataMapper.Configuration;
using MyBatis.DataMapper.Configuration.Interpreters.Config.Xml;
using Framework.Common;

namespace Framework.Mybatis
{
    class MybatisHelper
    {
        public static IDataMapper GetMapper(string dbKey)
        {
            var configurationSetting = new ConfigurationSetting();
            //configurationSetting.Properties.Add("collection2Namespace", "MyBatis.DataMapper.SqlClient.Test.Domain.LineItemCollection2, MyBatis.DataMapper.SqlClient.Test");
            //configurationSetting.Properties.Add("nullableInt", "int?");

            string resource = PathHelper.MapPath("sqlmap.config");

            IConfigurationEngine engine = new DefaultConfigurationEngine(configurationSetting);
            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));
            //engine.RegisterModule(new AliasModule());

            IMapperFactory mapperFactory = engine.BuildMapperFactory();
            //var sessionFactory = engine.ModelStore.SessionFactory;
            var dataMapper = ((IDataMapperAccessor)mapperFactory).DataMapper;
            //var sessionStore = ((IModelStoreAccessor)dataMapper).ModelStore.SessionStore;
            return dataMapper;
        }
    }
}
